<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
class Admin_model extends MY_Model
{
    private $table = 'admin';
	public function __construct()
	{
		parent::__construct($this->table);
	}
	
	/**
	 * 根据用户名和密码获取用户信息
	 *
	 * @param unknown $adminname
	 * @param unknown $password
	 */
    public function getAdminByName($adminName, $password = FALSE)
	{
	 
		if ($password != FALSE)
		{
			$this->db->where('password', md5($password));
		}
		return $this->db->where('adminName', $adminName)->get( $this->table )->row_array();

	}

	/**
	 * 获取用户列表
	 * */
	public function getAdminList($where=array(), $select = '*',$limit=10,$start=0)
	{
	    $param = $this->_getWhere($where);
	    $this->db->select($select);
	    $this->db->where($param);
	    $this->db->order_by('adminId','desc')->limit($limit, $start);
	    return $this->db->get( $this->table )->result_array();
	}
	
	/**
	 * 获取用户列表
	 * */
	public function getAdminAllList()
	{	 
	    $this->db->order_by('adminId','desc');
	    return $this->db->get($this->table)->result_array();
	}

	
	/**
	 *获取单条数据
	 * */
	public function getAdminRow($adminId=0)
	{
	    $this->db->where('adminId', $adminId);
	    $result = $this->db->get($this->table);
	    return $result->row_array();
	}
	
	/**
	 * 添加
	 * */
	public function insertData($userData)
	{
	    $res = $this->db->insert($this->table, $userData);
	    return $res;
	}
	
    /**
     * 修改
     * */

	public function setAdminByID($updateData, $adminId)
	{
	    $this->db->where('adminId', $adminId);
	    return $this->db->update($this->table, $updateData);
	}
	
	/**
	 * 获取总条数
	 * */
	public function getAdminCount($where=array())
	{
	    $param = $this->_getWhere($where);
	    $this->db->where($param);
	    return $this->db->count_all_results($this->table);
	}
	
	/**
	 * 查询条件
	 * */
	public function _getWhere( $where=array() )
	{
	    $param = array();
	    
	    if (!empty($where['realName']))
	    {
	        $param['realName like'] = '%'.strval($where['realName']).'%';
	    }
	    if (!empty($where['adminName']))
	    {
	        $param['adminName like'] = '%'.strval($where['adminName']).'%';
	    }
	
	    return $param;
	}
	
	/**
	 * 验证唯一
	 * */
	public function checkUnique($name,$adminId=0)
	{
	    $this->db->where('adminName', $name);
	    if(!empty($adminId))
	    {
	        $this->db->where('adminId !=', $adminId);
	    }
	    $result = $this->db->get($this->table);
	    return $result->row_array();
	}
	
	//获取数据条数
	public function getCount($search){
	    if($search){
	        $this->db->like('adminName',$search);
	        $this->db->or_like('realName',$search);
	    }
	    return $this->db->get($this->table)->num_rows();
	}
	//后台获取管理员数据
	public function getList_admin($page=0,$limit=20,$search=''){
	    //$this->db->where();
	    if($search){
	        $this->db->like('adminName',$search);
	        $this->db->or_like('realName',$search);
	    }
	    $this->db->select('admin.*,role.roleName');
	    $this->db->join('role','admin.roleId=role.roleId','left');
	    $res=$this->db->order_by('createTime','desc')->get($this->table,$limit,$page*$limit)->result_array();
	    return $res;
	}
	//新增数据
	public function add($data){
	    $data['createTime']=time();
	    $data['password']=md5($data['password']);
	    $this->db->insert($this->table,$data);
	    return $this->db->insert_id();
	}
	//更新数据
	public function update($id,$data){
	    $data['updateTime']=time();
	    $data['password']=md5($data['password']);
	    $this->db->where('adminId',$id)->update($this->table,$data);
	    return $this->db->affected_rows();
	}
	//删除单个或多个数据
	public function del($adminIds){
	    $this->db->where_in('adminId',$adminIds);
	    $this->db->delete($this->table);
	    return $this->db->affected_rows();
	}
	
	
}
?>